<?php
namespace app\appoint\controller;
use app\AdminRest;
use app\appoint\model\Question;
use app\appoint\model\QuestionList;
use app\appoint\model\QuestionPv;
use app\appoint\model\QuestionText;
use app\appoint\model\QuestionUserText;
use longbingcore\wxcore\Excel;
use think\App;
use think\facade\Db;
use think\Request;
use app\appoint\model\AdminCate as cate_model;
use app\appoint\model\AdminProject;

class AdminQuestion extends AdminRest
{

    protected $model;
    //预约服务模型
    protected $list_model;

    protected $text_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model      = new Question();

        $this->list_model = new QuestionList();

        $this->text_model = new QuestionText();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:02
     * @功能说明:问卷列表
     */
    public function questionList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $user_text_model = new QuestionUserText();

            foreach ($data['data'] as &$v){
                //回收量
                $v['recovery'] = $user_text_model->where(['que_id'=>$v['id'],'status'=>1])->count();

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:05
     * @功能说明:添加问卷
     */
    public function questionAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->dataAdd($input);

        $id  = $this->model->getLastInsID();

        return $this->success($id);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:09
     * @功能说明:问卷详情
     */
    public function questionInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:10
     * @功能说明:问卷编辑
     */
    public function questionUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:15
     * @功能说明:添加添加矩阵单选题
     */
    public function queTextAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;
        //查询一下有没有该排序值的题目 排序值不能一样
        $top_text = $this->list_model->where(['q_id'=>$input['q_id'],'top'=>$input['top'],'text_id'=>0])->find();

        if(!empty($top_text)){

            $this->errorMsg('已有该排序值的题目了，排序值必须唯一');

        }
        //增加头题目
        $res = $this->list_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 10:14
     * @功能说明:
     */
    public function queTextUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;
        //查询一下有没有该排序值的题目 排序值不能一样
        $top_text = $this->list_model->where('id','<>',$input['id'])->where(['q_id'=>$input['q_id'],'top'=>$input['top'],'text_id'=>0])->find();

        if(!empty($top_text)){

            $this->errorMsg('已有该排序值的题目了，排序值必须唯一');

        }
        //增加头题目
        $res = $this->list_model->dataUpdate($dis,$input);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-27 17:33
     * @功能说明:
     */
    public function queTextDel(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        //增加头题目
        $res = $this->list_model->where($dis)->delete();

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-01-25 18:04
     * @功能说明:文券详情
     */
    public function textListInfo(){

        $input = $this->_input;

        $dis = [

            'uniacid' => $this->_uniacid,

            'q_id'    => $input['id'],

            'text_id' => 0
        ];


        $data = $this->list_model->dataSelect($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-27 10:23
     * @功能说明:需要跳转的题目 只能跳排序值比自己小的
     *
     */
    public function jumpSelectText(){

        $input = $this->_input;

        $info  = $this->list_model->dataInfo(['id'=>$input['id']]);

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['top','<',$info['top']];

        $dis[] = ['q_id','=',$input['q_id']];

        $dis[] = ['text_id','=',0];

        $list  = $this->list_model->where($dis)->field('id,title')->order('top desc,id desc')->select()->toArray();

        return $this->success($list);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-28 10:03
     * @功能说明:问卷的统计信息
     */
    public function queForm(){

        $input = $this->_input;

        $pv_model = new QuestionPv();

        $user_text_model = new QuestionUserText();
        //浏览量
        $data['info']['pv'] = $pv_model->where(['q_id'=>$input['id']])->sum('pv');
        //回收量
        $data['info']['recovery'] = $user_text_model->where(['que_id'=>$input['id'],'status'=>1])->count();
        //线型图
        $data['date_list'] = $pv_model->where(['q_id'=>$input['id']])->limit(7)->order('date_num')->select()->toArray();

        if(!empty($data['date_list'])){

            foreach ($data['date_list'] as &$v){

                $v['week'] = date('w',$v['date_num']);

                $v['week'] = changeWeek($v['week']);

            }
        }

        $where[] = ['a.que_id','=',$input['id']];

        $where[] = ['a.status','=',1];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $where[] = ['a.create_time','between',"$start_time,$end_time"];
        }

        if(!empty($input['title'])){

            $where[] = ['b.nickName','like',"%".$input['title']."%"];

        }
        //提交记录
        $data['list'] = $user_text_model->userTextList($where,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-28 10:47
     * @功能说明:用户问卷详情
     */
    public function userQueInfo(){

        $input = $this->_input;

        $dis = [

            'text_id' => $input['id'],

            'uniacid' => $this->_uniacid
        ];

        $data = $this->list_model->where($dis)->order('top')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['data_text'] = $this->text_model->textInfo($v);
            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 10:14
     * @功能说明:导出用户提交的问卷
     */

    public function queExcel(){

        $input = $this->_param;

        $user_text_model = new QuestionUserText();
        //用户答题详情
        $where[] = ['a.que_id','=',$input['que_id']];

        $where[] = ['a.status','=',1];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $where[] = ['a.create_time','between',"$start_time,$end_time"];
        }

        if(!empty($input['title'])){

            $where[] = ['b.nickName','like',"%".$input['title']."%"];

        }

        if(!empty($input['id'])){

            $where[] = ['a.id','in',$input['id']];

        }
        //提交记录
        $user_text = $user_text_model->userTextSelect($where);

        $text_arr = [];

        if(!empty($user_text)){

            foreach ($user_text as $value){

                $dis = [

                    'text_id' => $value['id'],

                    'uniacid' => $this->_uniacid
                ];

                $data = $this->list_model->where($dis)->order('top')->select()->toArray();

                $arr   = ['ID','昵称'];

                $ans   = [$value['id'],$value['nickName']];

                if(!empty($data)){

                    foreach ($data as $k=>$v){

                        $v['data_text'] = $this->text_model->textInfo($v);

                        $num = $k+1;

                        if($v['type']==2||$v['type']==3){

                            $arr[] = $num.'、'.$v['title'];

                        }
                        //矩阵单选题
                        if($v['type']==1&&!empty($v['data_text'])){

                            foreach ($v['data_text'] as $vs){

                                $arr[] = $num.'、'.'('.$v['title'].')'.$vs['text'];

                            }

                        }
                        //答案
                        if($v['type']==3){

                            $ans[] = $v['ans_text'];

                        }
                        //联动单选题
                        if($v['type']==2){

                            $ans[] = $this->text_model->where(['q_id'=>$v['id'],'is_select'=>1])->value('text');

                        }
                        //矩阵单选题
                        if($v['type']==1){

                            $son_que = $this->text_model->where(['q_id'=>$v['id'],'type'=>1])->column('id');

                            if(!empty($son_que)){

                                foreach ($son_que as $vv){

                                    $ans[] = $this->text_model->where(['pid'=>$vv,'is_select'=>1])->value('text');

                                }
                            }

                        }

                    }

                }
                //问题
                $text_arr[] = $arr;
                //答案
                $text_arr[] = $ans;

                $text_arr[]  = [];

            }
        }
        //问卷标题
        $que_title = $this->model->where(['id'=>$input['que_id']])->value('title');

        $name = $que_title.'——问卷调查表';

        $excel_core = new Excel();

        $excel_core->excelExport($name,[],$text_arr);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-01-28 14:22
     * @功能说明:用户问卷导出
     */
    public function queExcels(){

        $input = $this->_input;

        $input['id'] = 2;

        $info = $this->model->dataInfo(['id'=>$input['id']]);

        $dis = [

            'uniacid' => $this->_uniacid,

            'q_id'    => $input['id'],

            'text_id' => 0
        ];
        //查看所有题目
        $data = $this->list_model->dataSelect($dis);

        $arr = [

            '编号',

            '昵称',

        ];

        $id_arr = [];

        foreach ($data as $k=>$v){

            $id_arr[] = $v['id'];

            $num = $k+1;

            if($v['type']==2||$v['type']==3){

                $arr[] = $num.'、'.$v['title'];

            }
            //矩阵单选题
            if($v['type']==1&&!empty($v['data_text'])){

                foreach ($v['data_text'] as $vs){

                    $arr[] = $num.'、'.$vs['text'];

                }

            }

        }

        $name  ='问卷调查列表';

        $user_text_model = new QuestionUserText();
        //用户答题详情
        $user_text = $user_text_model->where(['status'=>1,'que_id'=>$input['id']])->select()->toArray();

        $new_data = [];

        if(!empty($user_text)){

            foreach ($user_text as $k=>$v){
                //答题详情
                $dis = [

                    'text_id' => $v['id'],

                    'uniacid' => $this->_uniacid
                ];

                $ans = [];

                foreach ($id_arr as $vs){

                    $dis['text_p_id'] = $vs;

                    $text_info = $this->list_model->dataInfo($dis);
                    //填空题
                    if($text_info['type']==3){

                        $ans[] = $text_info['ans_text'];

                    }
                    //联动单选题
                    if($text_info['type']==2){

                        $ans[] = $this->text_model->where(['pid'=>$text_info['id'],'is_select'=>1])->value('text');
                    }

                    if($text_info['type']==1){

                        $son_ans = $this->text_model->where(['pid'=>$text_info['id'],'is_select'=>1])->column('text');

                        $ans = array_merge($ans,$son_ans);
                    }

                }

                $info   = array();

                $info[] = $v['id'];

                $info[] = Db::name('longbing_card_user')->where(['id'=>$v['user_id']])->value('nickName');

                $info = array_merge($info,$ans);

                $new_data[] = $info;

            }

        }

        dump($new_data);exit;

    }





}
